package cc.sjfx.sharedoc.dialog;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cc.sjfx.sharedoc.R;
import cc.sjfx.sharedoc.bean.MajorPickerBean;
import cc.sjfx.sharedoc.db.bmob.Major;
import cc.sjfx.sharedoc.function.ReadAndWriteFile;
import cc.sjfx.sharedoc.utils.CheckUtils;
import cc.sjfx.sharedoc.utils.StrUtils;
import cc.sjfx.sharedoc.widget.view.PickerView;
import cc.sjfx.sharedoc.widget.view.PickerView.onSelectListener;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;

public class MajorPickerDialog extends Dialog implements onSelectListener {
	PickerView majorPicker1;
	PickerView majorPicker2;
	Button btn_major;
	String[] majorNames;
	String majorName;
	int mid;
	int[] mids;
	int cid;
	String major_class;
	Context context;

	public MajorPickerDialog(Context context) {
		super(context, R.style.MyDialog);
		this.context = context;
		// TODO Auto-generated constructor stub
		setContentView(R.layout.dialog_setmajor);
		majorPicker1 = (PickerView) findViewById(R.id.majorPicker1);
		majorPicker2 = (PickerView) findViewById(R.id.majorPicker2);
		majorPicker2.setData(new String[] { "请选择" });
		majorPicker1.setData(StrUtils.MAJOR_CLASS);
		btn_major = (Button) findViewById(R.id.btn_major);
		getMajor(6);
		majorPicker1.setOnSelectListener(this);
		majorPicker2.setOnSelectListener(this);
	}

	public void setDialogListener(
			final DialogListener<MajorPickerBean> dialogListener) {
		btn_major.setOnClickListener(new android.view.View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				MajorPickerBean bean = new MajorPickerBean(majorName, mid,
						major_class, cid);
				dialogListener.buttonClick(bean);
				dismiss();
			}
		});
	}

	@Override
	public void onSelect(PickerView pickerView, String string,
			int mCurrentSelected) {
		// TODO Auto-generated method stub
		switch (pickerView.getId()) {
		case R.id.majorPicker1:
			getMajor(mCurrentSelected);
			break;
		case R.id.majorPicker2:
			mid = mids[mCurrentSelected];
			majorName = majorNames[mCurrentSelected];
			break;
		default:
			break;
		}
	}

	private void getMajor(final int mCurrentSelected) {
		// TODO Auto-generated method stub
		major_class = StrUtils.MAJOR_CLASS[mCurrentSelected];
		cid = StrUtils.CID[mCurrentSelected];
		final ReadAndWriteFile file = new ReadAndWriteFile(context);
		String data = file.readFile(StrUtils.MAJOR_FILE + cid);
		if (CheckUtils.stringIsNull(data)) {
			BmobQuery<Major> query = new BmobQuery<Major>();
			query.addWhereEqualTo("belong", cid);
			query.setLimit(1000);
			query.findObjects(context, new FindListener<Major>() {
				@Override
				public void onSuccess(List<Major> arg0) {
					// TODO Auto-generated method stub
					JSONArray jsonArray = new JSONArray();
					for (Major major : arg0) {
						JSONObject jsonObject = new JSONObject();
						try {
							jsonObject.put("name", major.getName());
							jsonObject.put("mid", major.getMid());
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						jsonArray.put(jsonObject);
					}
					file.writeFile(StrUtils.MAJOR_FILE + cid,
							jsonArray.toString());
					getMajor(mCurrentSelected);
				}

				@Override
				public void onError(int arg0, String arg1) {
					// TODO Auto-generated method stub
				}
			});
		} else {
			int maxM = 3;
			try {
				JSONArray jsonArray = new JSONArray(data);
				int len = jsonArray.length();
				majorNames = new String[len];
				mids = new int[len];
				for (int i = 0; i < len; i++) {
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					majorNames[i] = jsonObject.getString("name");
					maxM = maxM > majorNames[i].length() ? maxM : majorNames[i]
							.length();
					mids[i] = jsonObject.getInt("mid");
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			majorPicker2.setData(majorNames);
//			majorPicker2.setTextSize(100 * 4 / maxM, 40 * 4 / maxM);
			mid = mids[majorNames.length/2];
			majorName = majorNames[majorNames.length/2];
		}
	}
}
